package com.zz.ecommerce.declareManage.importB2CDeclareDirect.exchangeQdc;

import com.zz.ecommerce.common.GridRequest;
import com.zz.ecommerce.common.GridResult;
import com.zz.ecommerce.common.bean.qdc.db.TDeclformHeadQdc;
import com.zz.ecommerce.common.constants.CommonConstants;
import com.zz.ecommerce.common.constants.TableConstants;
import com.zz.ecommerce.declareManage.common.dao.TDeclformHeadQdcDao;
import com.zz.ecommerce.declareManage.importB2CDeclareDirect.exchange.DirectExchangeFileDao;
import com.zz.ecommerce.declareManage.importB2CDeclareDirect.exchange.DirectExchangeGoodsDao;
import com.zz.ecommerce.pojo.CbecExchangeGoodsInfo;
import com.zz.ecommerce.pojo.CbecExchangeList;
import com.zz.ecommerce.utils.SessionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by zhangguowei on 2016/3/28.
 * <p/>
 * 退换货管理
 */
@Controller
@RequestMapping("/importB2CDirect/exchangeQdc")
@Scope("prototype")
public class DirectExchangeQdcController {

    @Autowired
    private DirectExchangeFileDao exchangeFileDao;

    @Autowired
    private DirectExchangeGoodsDao exchangeGoodsDao;
    @Autowired
    private TDeclformHeadQdcDao tDeclformHeadQdcDao;

    /**
     * 退货申请单生成查询
     *
     * @return
     */
    @RequestMapping(value = "createIndx", method = {RequestMethod.GET,RequestMethod.POST})
    public String createIndx() {
        return "declareManage/importB2CDeclareDirect/exchangeQdc/exchange_create_indx";
    }

    /**
     * 退货申请单生成查询
     *
     * @return
     */
    @RequestMapping(value = "indx", method = {RequestMethod.GET,RequestMethod.POST})
    public String indx() {
        return "declareManage/importB2CDeclareDirect/exchangeQdc/indx";
    }

    /**
     * 可退单申请单查询
     *
     * @param tDeclformHeadQdc
     * @param request
     * @return
     */
    @RequestMapping("/queryCreateExchangeList")
    @ResponseBody
    public GridResult queryDeclareList(TDeclformHeadQdc tDeclformHeadQdc, GridRequest request,HttpSession session) {
        tDeclformHeadQdc.setTradeMode(CommonConstants.B2C_DIRECT);
//        tDeclformHeadQdc.setReturnFlag(CommonConstants.ZERO);
        tDeclformHeadQdc.setIeFlag(CommonConstants.I_FALG);
        tDeclformHeadQdc.setOrgId(SessionUtil.getOrganizationId(session));
        tDeclformHeadQdc.setReturnFlag(CommonConstants.ONE);
        return new GridResult(tDeclformHeadQdcDao.page(TableConstants.T_DECLFORM_HEAD_QDC, tDeclformHeadQdc, request.getStart(), request.getEnd(), request.getSortOrder()),
                request, tDeclformHeadQdcDao.count(TableConstants.T_DECLFORM_HEAD_QDC, tDeclformHeadQdc));
    }

    /**
     * 退货申请单查询
     *
     * @return
     */
    @RequestMapping(value = "searchIndx", method = {RequestMethod.GET})
    public String searchIndx() {
        return "declareManage/importB2CDeclareDirect/exchangeQdc/exchange_search_indx";
    }

    /**
     * 查询申报单列表
     *
     * @param exchangeList
     * @param request
     * @return
     */
    @RequestMapping("/queryExchangeList")
    @ResponseBody
    public GridResult queryExchangeList(CbecExchangeList exchangeList, GridRequest request) {
        exchangeList.setTradeMode(CommonConstants.B2C_DIRECT);
        int count = exchangeFileDao.count(TableConstants.CBEC_EXCHANGE_LIST, exchangeList);
        List<CbecExchangeList> orderHeadList = exchangeFileDao.page(TableConstants.CBEC_EXCHANGE_LIST, exchangeList,
                request.getStart(), request.getEnd(), request.getSortOrder());
        return new GridResult(orderHeadList, request, count);
    }

    /**
     * 退货申请单查询
     *
     * @return
     */
    @RequestMapping(value = "detail/{exId}", method = {RequestMethod.GET})
    public String searchIndx(@PathVariable("exId")String exId,ModelMap modelMap) {
        modelMap.addAttribute("exId",exId);
        return "declareManage/commonQdc/exchange_search_detail";
    }


    /**
     * 退换货详情 信息取得
     * @param exchangeList
     * @param request
     * @return
     */
    @RequestMapping("/getExchangeInfo")
    @ResponseBody
    public CbecExchangeList getExchangeInfo(CbecExchangeList exchangeList, GridRequest request) {
        return exchangeFileDao.qryOneCommon(TableConstants.CBEC_EXCHANGE_LIST,exchangeList,"");
    }

    /**
     * 查询申报单列表
     *
     * @param cbecExchangeGoodsInfo
     * @param request
     * @return
     */
    @RequestMapping("/qryGoodsInfo")
    @ResponseBody
    public GridResult qryGoodsInfo(CbecExchangeGoodsInfo cbecExchangeGoodsInfo, GridRequest request) {
        int count = exchangeGoodsDao.count(TableConstants.CBEC_EXCHANGE_GOODS_INFO, cbecExchangeGoodsInfo);
        List<CbecExchangeGoodsInfo> orderHeadList = exchangeGoodsDao.page(TableConstants.CBEC_EXCHANGE_GOODS_INFO, cbecExchangeGoodsInfo,
                request.getStart(), request.getEnd(), request.getSortOrder());
        return new GridResult(orderHeadList, request, count);
    }
}
